<?php
$adm_menu = "1";
include("config.php");
if (!isset($_SESSION["auser"]) || $_SESSION["auser"] == "" || $_SESSION['agroup'] != "WEB_ADMIN") 
{
header('Location: index.php');
die();
}

$db_rdata = new DB($rdata_server_username,$rdata_server_pass,$rdata_server_db,$rdata_server,$rdata_driver);
$db_www = new DB($server_username,$server_pass,$server_db,$server,$driver);

$err = "";
if (!isset($_REQUEST["imcode"])) {
	$imcode = "";			
} else {
	$imcode = $_REQUEST["imcode"];			
}
if (!isset($_REQUEST["tipas"])) {
	$tipas = "";			
} else {
	$tipas = $_REQUEST["tipas"];			
}
if (!isset($_REQUEST["prcode"])) {
	$prcode = "";			
} else {
	$prcode = $_REQUEST["prcode"];			
}
if (!isset($_REQUEST["impav"])) {
	$impav = "";			
} else {
	$impav = $_REQUEST["impav"];			
}
if (!isset($_REQUEST["prpav"])) {
	$prpav = "";			
} else {
	$prpav = $_REQUEST["prpav"];			
}
if (isset($_REQUEST["findim"])){
	if ($_REQUEST["findim"] == "1") {
		$test_q = "SELECT TOP 1 N08_kodas_ks,N08_PAV FROM n08_klij WHERE N08_kodas_ks = '".addslashes_mssql($_REQUEST["imcode"])."'";

		$im = $db_www->returnInfo($test_q);	
		if ($im == null) {
			$impav = "";
			
			$err .= "<br>Nerasta įmonė su tokiu kodu!";
		} else {
			$impav = iconv('windows-1257','UTF-8', $im["N08_PAV"]);
			
		}
	} else if ($_REQUEST["findim"] == "2") {
		$test_q = "SELECT TOP 1 n17_kodas_ps,N17_PAV FROM n17_prod WHERE n17_kodas_ps = '".addslashes_mssql($_REQUEST["prcode"])."'";
		$pr = $db_www->returnInfo($test_q);	
		if ($pr == null) {
			$test_q = "SELECT TOP 1 n25_kodas_bs,N25_PAV FROM N25_KOD WHERE n25_kodas_bs = '".addslashes_mssql($_REQUEST["prcode"])."'";
			$pr = $db_www->returnInfo($test_q);	
			if ($pr == null) {		
				$prpav = "";				
				$err .= "<br>Nerastas priedas su tokiu kodu!";
			} else {
				$prpav = iconv('windows-1257','UTF-8', $pr["N25_PAV"]);				
			}				
		} else {
			$prpav = iconv('windows-1257','UTF-8', $pr["N17_PAV"]);
			
		}
	}
}


if (isset($_REQUEST["act"]) && !isset($_REQUEST["findim"])){
	if ($_REQUEST["act"] == "del" && isset($_REQUEST["id"])){
		$rdel_query = str_replace("<id>", addslashes_mssql($_REQUEST["id"]), $rdel_query);
		$db_rdata->execute($rdel_query);		
	} else if ($_REQUEST["act"] == "save") {
		//ar yra toks imones kodas
		$test_q = "SELECT N08_kodas_ks,N08_PAV FROM n08_klij WHERE N08_kodas_ks = '".$_REQUEST["imcode"]."'";
		$im = $db_www->returnInfo($test_q);		
		if (empty($im)) {
			$err .= "<br>Nerasta įmonė su tokiu kodu!";
		} else {
			$impav=iconv('windows-1257','UTF-8', $im["N08_PAV"]);
		}
		$test_q = "SELECT TOP 1 n17_kodas_ps,N17_PAV FROM n17_prod WHERE n17_kodas_ps = '".addslashes_mssql($_REQUEST["prcode"])."'";
		$pr = $db_www->returnInfo($test_q);	
		if ($pr == null) {
			$test_q = "SELECT TOP 1 n25_kodas_bs,N25_PAV FROM N25_KOD WHERE n25_kodas_bs = '".addslashes_mssql($_REQUEST["prcode"])."'";
			$pr = $db_www->returnInfo($test_q);	
			if ($pr == null) {								
				$err .= "<br>Nerastas priedas su tokiu kodu!";
			} else {
				$prpav =  iconv('windows-1257','UTF-8', $pr["N25_PAV"]);	
			}			
		}  else {
			$prpav = iconv('windows-1257','UTF-8', $pr["N17_PAV"]);
		}
		//jei nebuvo klaidu
		if ($err == ""){
			$sql = "exec web_insert_register
			@imcode='". addslashes_mssql($_REQUEST["imcode"])."'
			,@impav='". addslashes_mssql(ltCharsToEntities($impav))
			."',@prcode='".addslashes_mssql($_REQUEST["prcode"])."'
			,@prpav='".addslashes_mssql(ltCharsToEntities($prpav))
			
			."',@tipas='".addslashes_mssql($_REQUEST["tipas"])."';";
			$db_rdata->execute($sql);
			
			header('Location: register.php');
		}
		
	}
}


if (isset($_REQUEST['month'])){
	$month = $_REQUEST['month'];
} else {
	$month = date("m", strtotime("-1 months"));
}


if (isset($_REQUEST['year'])){
	$year = $_REQUEST['year'];
} else {
	$year = date("Y");
}





include("header.php");
include("menu.php");


?>
<script type="text/javascript">
	$(document).ready(function(){	

		if (!$.browser.opera) {
    
			// select element styling
			$('select.select').each(function(){
				var title = $(this).attr('title');
				if( $('option:selected', this).val() != ''  ) title = $('option:selected',this).text();
				$(this)
					.css({'z-index':10,'opacity':0,'-khtml-appearance':'none'})
					.after('<span class="select">' + title + '</span>')
					.change(function(){
						val = $('option:selected',this).text();
						$(this).next().text(val);
						})
			});

		};
		
	});
	

function unhide() {
	but = document.getElementById('newbut');
	form = document.getElementById('newform');
	but.style.display = 'none';
	form.style.display = 'inline';
}

function hide() {
	but = document.getElementById('newbut');
	form = document.getElementById('newform');
	but.style.display = 'inline';
	form.style.display = 'none';
}

	
</script>


<?php
	if (isset($_REQUEST["act"]) && $_REQUEST["act"]=="save") {
		$disp = "";
		$disp2 = "display:none;";
	} else {
		$disp = "display:none;";
		$disp2 = "";	
	}
	$f = "<form id='newbut' action='register.php' method=\"post\" style=". $disp2 .">";
	$f .= '<table cellspacing="0" cellpadding="5" class="params"><tbody><tr><td style="text-align:center;"><a id="newButton" onclick="unhide();"  class="card3">Naujas registro įrašas</a></td></tr></tbody></table><br>';

	$f .= "<table cellspacing='0' cellpadding='5' class='params'>";
	
	$f .= "<tr><th>Pasirinkite periodą:</th></tr>";
	
	
		$f .= "<tr><th>Metai:</th></tr>";
		$f .= "<tr><td style='text-align:left;'><input  class=\"inp\" type='text' name=\"year\" value=\"" . $year . "\"></td></tr>";
		
	$f .= "<tr><th>Mėnuo:</th></tr>";
	$f .= "<tr><td style='text-align:left;'>";
        $f .= '<div>';
        $f .= '<select name="month" class="select" >';
		$f .= "<option value=\"1\"";  if ($month=="1") {$f .= 'selected="selected"';}$f .= ">Sausis</option>";
		$f .= "<option value=\"2\"";  if ($month=="2") {$f .= 'selected="selected"';}$f .= ">Vasaris</option>";
		$f .= "<option value=\"3\"";  if ($month=="3") {$f .= 'selected="selected"';}$f .= ">Kovas</option>";
		$f .= "<option value=\"4\"";  if ($month=="4") {$f .= 'selected="selected"';}$f .= ">Balandis</option>";
		$f .= "<option value=\"5\"";  if ($month=="5") {$f .= 'selected="selected"';}$f .= ">Gegužė</option>";
		$f .= "<option value=\"6\"";  if ($month=="6") {$f .= 'selected="selected"';}$f .= ">Birželis</option>";
		$f .= "<option value=\"7\"";  if ($month=="7") {$f .= 'selected="selected"';}$f .= ">Liepa</option>";
		$f .= "<option value=\"8\"";  if ($month=="8") {$f .= 'selected="selected"';}$f .= ">Rugpjūtis</option>";
		$f .= "<option value=\"9\"";  if ($month=="9") {$f .= 'selected="selected"';}$f .= ">Rugsėjis</option>";
		$f .= "<option value=\"10\"";  if ($month=="10") {$f .= 'selected="selected"';}$f .= ">Spalis</option>";
		$f .= "<option value=\"11\"";  if ($month=="11") {$f .= 'selected="selected"';}$f .= ">Lapkritis</option>";
		$f .= "<option value=\"12\"";  if ($month=="12") {$f .= 'selected="selected"';}$f .= ">Gruodis</option>";

		$f .= '</select>';
        $f .= '</div>		';
	$f .= "</td></tr>";
	$f .= "<tr><td ><input class='but bgfilter' type = \"submit\" value = \"Rodyti\"></td></tr>";
	$f .= "</table></form>";

	$d = "";
for ($i = 1; $i <= 2; $i++) {
	$pirkimai = $db_rdata->returnList("select * from n90_registras where n90_tipas = $i");	


	if (!empty($pirkimai)) {

		$d .= '<p>';
		if ($i == 1) {
			$d .= 'Pirkimai';
		} else {
			$d .= 'Pardavimai';
		}
		
		$d .= '</p><table cellspacing="0" cellpadding="0" width="100%" border = "0">';
		$d .= '<tr><td align="center">';
		$d .= '<table cellspacing="0" cellpadding="3" class="innercont2">';
		
			$d .= '<tr><th width=15>Eil. nr.</th><th width=50>Kliento kodas</th><th>Kliento pavadinimas</th><th width=40>Priedo kodas</th><th>Priedo pavadinimas</th><th>Sąskaitos nr.</th><th width=50>Sąskatos data</th><th>Suma</th></tr>';
		
		foreach ($pirkimai as $pirk) {
			$d .= "<tr>";
			$d .= "<td><A class='card' href='reg.php?code=".$pirk["N90_id"]."'>".$pirk["N90_eil_nr"]."</A></td>";
			$d .= "<td><A class='card' href='reg.php?code=".$pirk["N90_id"]."'>".$pirk["N90_kodas_ks"]."</A></td>";
			$d .= "<td><A class='card' href='reg.php?code=".$pirk["N90_id"]."'>".htmlspecialchars(ltCharsFromEntities($pirk["N90_pav"]))."</A></td>";
			$d .= "<td><A class='card' href='reg.php?code=".$pirk["N90_id"]."'>".$pirk["N90_kodas"]."</A></td>";
			$d .= "<td><A class='card' href='reg.php?code=".$pirk["N90_id"]."'>".htmlspecialchars(ltCharsFromEntities($pirk["N90_pav1"]))."</A></td>";
			$saskaita = $db_www->returnInfo("SELECT TOP 1
	p.I06_DOK_NR,
	CONVERT(VARCHAR(10),p.I06_OP_DATA, 102) as I06_OP_DATA
	,p.i06_suma
	FROM [I06_PARH] p, [I07_PARD] d
	  where p.I06_KODAS_PO=d.[I07_KODAS_PO]
	and p.I06_KODAS_KS='".addslashes_mssql($pirk["N90_kodas_ks"])."'
	and d.I07_KODAS='".addslashes_mssql($pirk["N90_kodas"])."'
	and month(p.I06_OP_DATA)='".addslashes_mssql($month)."'
	and year(p.I06_OP_DATA)='".addslashes_mssql($year)."'
	order by p.I06_OP_DATA desc");	
	if (!empty($saskaita)) {
		$d .= "<td>".$saskaita["I06_DOK_NR"]."</td>";
		$d .= "<td>".$saskaita["I06_OP_DATA"]."</td>";
		$d .= "<td nowrap><div style='float:right;'>".$saskaita["i06_suma"]." <font style='text-align:right;color:green;'>♦</font></div></td>";
	} else {
		$d .= "<td></td><td></td><td><div style='float:right;'><font style='color:red;'>♦</font></div></td>";
	}
		}

		$d .= '</table>';
		$d .= '</td>';
		$d .= '</tr></table>';

	}

}

?>

<table class="innercont" cellspacing="0" cellpadding="10">
<tr><td class="leftcol">
<?php
		if ($err != ""){
			echo "<p style='color:red;font-weight:bold;'>".$err."</p>";
		}
?>
<form method="post" name="forma" action="register.php?act=save">
<input id="poz" type="hidden" name="poz" value="new">
<input id="code" type="hidden" name="code" value="0">
<table id="newform" cellspacing="0" cellpadding="5" class="params" style="<?php echo $disp;?>">
<tbody><tr><th class="head">Naujas registro įrašas</th></tr>
<tr><th>Kliento kodas:</th></tr>
<tr><td style="text-align:left;"><input id="imcode" class="inp" type="text" name="imcode" value="<?php echo htmlspecialchars($imcode);?>"></td></tr>
<tr><td style="padding-right:20px;"><a class="flink" href="#" onclick="document.forma.action='register.php?act=save&findim=1';document.forma.submit()">Ieškoti</a></td></tr>
<tr><th>Kliento pavadinimas:</th></tr>
<tr><td style="text-align:left;"><input id="impav" class="inp" type="text" name="impav" value="<?php echo htmlspecialchars(ltCharsFromEntities($impav));?>" readonly="readonly"></td>
</tr>
<tr><th>Priedo kodas:</th></tr>
<tr><td style="text-align:left;"><input id="prcode" class="inp" type="text" name="prcode" value="<?php echo htmlspecialchars($prcode);?>"></td></tr>
<tr><td style="padding-right:20px;"><a class="flink" href="#" onclick="document.forma.action='register.php?act=save&findim=2';document.forma.submit()">Ieškoti</a></td></tr>
<tr><th>Priedo pavadinimas:</th></tr>
<tr><td style="text-align:left;"><input id="prpav" class="inp" type="text" name="prpav" value="<?php echo htmlspecialchars(ltCharsFromEntities($prpav));?>" readonly="readonly"></td>
</tr>


<tr><th>Tipas:</th></tr>
<tr><td style="text-align:left;">
<div><select name="tipas" class="select" >
<option value="1" <?php if ($tipas=="1") echo "SELECTED";?>>Pirkimas</option>
<option value="2" <?php if ($tipas=="2") echo "SELECTED";?>>Pardavimas</option>
</select>
</div>

</td></tr>



<tr><td style="text-align:center;">
<table border="0" cellspacin="0" cellpadding="0">
<tbody><tr><td style="text-align:left;padding:0;width:50px;">
<button id="unblockButton" class="but bgfilter nopad" onclick="hide();return false;">Atgal</button></td><td align="right" style="padding:0;width:80px;"><input class="but bgfilter nopad" type="submit" value="Išsaugoti"></td></tr></tbody></table></td></tr></tbody>
</table>
</form>
<?php

echo $f;




?>
</td><td class="rightcol" rowspan="2">
<?php

	echo $d;

?>
</td></tr>
<tr><td >

</td></tr>
</table>
<?php
include("footer.php");
?>